package jrmob.yonyou.com.commonlib.view.auto;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import jrmob.yonyou.com.commonlib.R;
import jrmob.yonyou.com.commonlib.app_config_tool.tool.base.CommonUtil;


/**
 * AutoCompleteEditView(带自动提示功能的EditText)
 * 
 * @author zhangg
 * 
 *         使用方法: tv = (AutoCompleteEditView) findViewById(R.id.tv);
 *         tv.setThreshold(0); adapter = new AutoCompleteAdapter(this,
 *         mOriginalValues, 10); tv.setAdapter(adapter);
 * 
 */
public class AutoCompleteEditView extends RelativeLayout {

	private Context context;
	private AutoCompleteTextView tv;

	public AutoCompleteTextView getTv() {
		return tv;
	}

	public void setTv(AutoCompleteTextView tv) {
		this.tv = tv;
	}

	private ImageView minageview; // 左边图标
	private ImageView iv; // 右边图标

	public ImageView getIv() {
		return iv;
	}

	public void setIv(ImageView iv) {
		this.iv = iv;
	}

	public ImageView getMinageview() {
		return minageview;
	}

	public void setMinageview(ImageView minageview) {
		this.minageview = minageview;
	}

	public AutoCompleteEditView(Context context) {
		super(context);
		this.context = context;
	}

	public AutoCompleteEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		initViews();
	}

	@SuppressWarnings("deprecation")
	private void initViews() {
		// EditText
		LayoutParams params = new LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		tv = new AutoCompleteTextView(context);
		tv.setLayoutParams(params);
		tv.setPadding(90, 0, 40, 0);
		tv.setSingleLine(true);
		tv.setHint("搜索");
		tv.setBackground(getResources()
				.getDrawable(R.drawable.bg_auto_edittext_lib));
		// 左边的图标
		LayoutParams mLayoutParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		mLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
		mLayoutParams.leftMargin = 15;
		minageview = new ImageView(context);
		minageview.setLayoutParams(mLayoutParams);
		minageview.setScaleType(ScaleType.FIT_CENTER);
		minageview.setImageResource(R.mipmap.search_background3x_ma);
		minageview.setClickable(true);

		// 右边的图标(删除)
		LayoutParams p = new LayoutParams(
				CommonUtil.dip2px(context,20),
				CommonUtil.dip2px(context,20));
		p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		p.addRule(RelativeLayout.CENTER_VERTICAL);
		p.rightMargin = 10;
		iv = new ImageView(context);
		iv.setLayoutParams(p);
		iv.setScaleType(ScaleType.FIT_CENTER);
		iv.setClickable(true);
		iv.setImageResource(R.drawable.selector_autoedittext_lib);

		this.addView(tv);
		this.addView(minageview);
		this.addView(iv);

	}

	public void setAdapter(AutoCompleteAdapter adapter) {
		tv.setAdapter(adapter);
	}

	public void setThreshold(int threshold) {
		tv.setThreshold(threshold);
	}

	public AutoCompleteTextView getAutoCompleteTextView() {
		return tv;
	}
}
